package com.briup.facade.web.controller;

import com.briup.common.web.Message;
import com.briup.common.web.MessageUtil;
import com.briup.facade.service.IFascadeTrainingService;
import com.briup.logging.annotation.LoggingAccess;
import com.briup.model.entity.extend.TrainingVo;
import com.briup.model.entity.extend.TraningEx;
import com.briup.model.service.ITrainingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@LoggingAccess("训练模块")
@Api(tags = "训练模块")
@RestController
@RequestMapping("/training")
public class TrainingController {
    @Autowired
    private IFascadeTrainingService fascadeTrainingService;
    @Autowired
    private ITrainingService trainingService;

    // 模型训练
    @LoggingAccess("模型训练")
    @ApiOperation(value = "模型训练",notes = "参数类型为json+resultful风格")
    @PostMapping("/{modelId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "modelId",value = "被训练模型id",paramType = "path",dataType = "int",required = true)
    })
    public Message trainingModel(@RequestBody TrainingVo trainingVo, @PathVariable("modelId") Integer modelId){
        fascadeTrainingService.trainingModel(trainingVo,modelId);
        return MessageUtil.success("已提交训练,请等待训练结果");
    }

    // 取消模型训练任务
    @LoggingAccess("取消训练")
    @ApiOperation(value = "取消训练",notes = "输出节点数据,改变训练状态")
    @PutMapping("/{modelId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "modelId",value = "被训练模型id",paramType = "path",dataType = "int",required = true)
    })
    public Message cancelTraining(@PathVariable("modelId") Integer modelId){
        fascadeTrainingService.cancelTraining(modelId);
        return MessageUtil.success("取消训练");
    }

    // 根据模型id查找训练记录中
    @LoggingAccess("根据模型id查找模型记录")
    @ApiOperation(value = "根据模型id查找训练记录",notes = "所有训练记录")
    @GetMapping("/{modelId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "modelId",value = "被训练模型id",paramType = "path",dataType = "int",required = true)
    })
    public Message findTrainingByModelId(@PathVariable("modelId") Integer modelId){
        List<TraningEx> traningExList = trainingService.findTrainingByModelId(modelId);
        return MessageUtil.success(traningExList);
    }

}
